<template>
  <el-card shadow="never">
    <div slot="header">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item :to="{ path: '/' }"> <i class="el-icon-house"></i> 首页</el-breadcrumb-item>
        <el-breadcrumb-item>数据统计</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <!-- 容器 -->
    <!-- 为 ECharts 准备一个具备大小（宽高）的 DOM -->
    <div ref="main"
         style="width: 600px;height:400px;"></div>
  </el-card>

</template>

<script>
// echarts 按需导出的！没有默认导出内容
import * as echarts from 'echarts'
import _ from 'lodash'
export default {
  name:'Report',
  async mounted () {
  // 基于准备好的dom，初始化echarts实例
    var myChart = echarts.init(this.$refs.main)

    // 指定图表的配置项和数据
    var option = {
      title: {
        text: '用户来源'
      },
      tooltip: {
        trigger: 'axis',
        axisPointer: {
          type: 'cross',
          label: {
            backgroundColor: '#E9EEF3'
          }
        }
      },
      grid: {
        left: '3%',
        right: '4%',
        bottom: '3%',
        containLabel: true
      },
      xAxis: [
        {
          boundaryGap: false
        }
      ],
      yAxis: [
        {
          type: 'value'
        }
      ]
    }

    // 请求后端数据
    const { data:res } = await this.$axios.get('/reports/type/1')
    if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
    // 合并数据
    option = _.assign(option, res.data)
    // 使用刚指定的配置项和数据显示图表。
    myChart.setOption(option)

    let newdata = '{"title":{"text":"用户来源"},"tooltip":{"trigger":"axis","axisPointer":{"type":"cross","label":{"backgroundColor":"#E9EEF3"}}},"grid":{"left":"3%","right":"4%","bottom":"3%","containLabel":true},"xAxis":[{"data":["2017-12-27","2017-12-28","2017-12-29","2017-12-30","2017-12-31","2018-1-1"]}],"yAxis":[{"type":"value"}],"legend":{"data":["华东","华南","华北","西部","其他"]},"series":[{"name":"华东","type":"line","stack":"总量","areaStyle":{"normal":{}},"data":[1555,3111,3333,3565,3528,4444]},{"name":"华南","type":"line","stack":"总量","areaStyle":{"normal":{}},"data":[5090,2500,3400,113,34444,7800]},{"name":"华北","type":"line","stack":"总量","areaStyle":{"normal":{}},"data":[6228,4060,8010,12321,13928,12984]},{"name":"西部","type":"line","stack":"总量","areaStyle":{"normal":{}},"data":[9991,41,7777,129,13098,140]},{"name":"其他","type":"line","stack":"总量","areaStyle":{"normal":{}},"data":[15212,58,10241,14821,15982,14091]}]}'
    newdata = JSON.parse(newdata)
    setTimeout(() => {
      console.log('更新啊！')
      myChart.setOption(newdata)
    }, 4000)
  }
}
// vue-echarts 自主学习一下！
</script>

<style>

</style>
